package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;

import entity.Order_Cart;
import entity.Product_Info;
import entity.Product_Pic_Info;
import entity.User;
import service.OrderCartService;
import service.ProductInfoService;
import service.ProductPicInfoService;
import service.UserService;
import service.impl.OrderCartServiceImpl;
import service.impl.ProductInfoServiceImpl;
import service.impl.ProductPicInfoServiceImpl;
import service.impl.UserServiceImpl;


public class BanlanceServlet extends HttpServlet {
	private OrderCartService ocs = new OrderCartServiceImpl();
	private UserService us = new UserServiceImpl();
	private ProductInfoService pis = new ProductInfoServiceImpl();
	private ProductPicInfoService ppis = new ProductPicInfoServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		Cookie cookie = new Cookie("loginInfo", "章敏"+",111");
		//和DoOrderCarServlet一样的方法
		if(cookie.getName().equals("loginInfo")){
			String loginInfo = cookie.getValue();
			String username = loginInfo.split(",")[0];
            request.setAttribute("username", username);
            User user = us.queryUserByName(username);
            List<Order_Cart> allOrder = ocs.queryAllOrder_CartListByUserId(user.getUserId());
            List<Product_Info> allProductInfo = new ArrayList<Product_Info>();
            List<Product_Pic_Info> allProductPicInfo = new ArrayList<Product_Pic_Info>();
            for (Order_Cart order_Cart : allOrder) {
    			Product_Info product_Info = pis.queryProduct_InfoById(order_Cart.getProduct_id());
    			Product_Pic_Info product_Pic_Info = ppis.getProduct_Pic_InfoById(order_Cart.getProduct_id());
    			if (product_Info != null && product_Pic_Info != null) {
    				allProductInfo.add(product_Info);
    				allProductPicInfo.add(product_Pic_Info);
    				//将当前的图片信息加入到产品信息中
    				product_Info.setProduct_Pic_Info(product_Pic_Info);
    				//将信息存入当前到购物车订单中
    				order_Cart.setProduct_Info(product_Info);
    			}
    		}
            response.getWriter().print(JSON.toJSONString(allOrder));
		}
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
